package com.pm.web.controller.buglist;


import com.pm.buglist.domain.ExceptionCode;
import com.pm.buglist.service.ExceptionCodeService;
import com.pm.common.annotation.Log;
import com.pm.common.core.controller.BaseController;
import com.pm.common.core.domain.AjaxResult;
import com.pm.common.core.page.TableDataInfo;
import com.pm.common.core.redis.RedisCache;
import com.pm.common.enums.BusinessType;
import com.pm.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * <p>
 * 异常代码管理表 前端控制器
 * </p>
 *
 * @author wwm
 * @since 2023-12-14
 */
@RestController
@RequestMapping("/business/exception-code")
public class ExceptionCodeController extends BaseController {

    @Resource
    private ExceptionCodeService exceptionCodeService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询异常代码管理列表
     */
//    @PreAuthorize("@ss.hasPermi('business:code:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExceptionCode exceptionCode)
    {
        startPage();
        List<ExceptionCode> list = exceptionCodeService.selectExceptionCodeList(exceptionCode);
        return getDataTable(list);
    }

    /**
     * 查询异常代码数据
     * */
    @GetMapping("/codeList")
    public AjaxResult getCodeList(){
        String key ="EX_CODE";
        List<Object> cacheList = redisCache.getCacheList(key);
        if(CollectionUtils.isEmpty(cacheList)){
            ReentrantLock locks = new ReentrantLock();
            try {
                //加锁
                locks.lock();
                cacheList = redisCache.getCacheList(key);
                if(CollectionUtils.isEmpty(cacheList)){
                    List<ExceptionCode> list = exceptionCodeService.selectExceptionCodeList(null);
                    if(!CollectionUtils.isEmpty(list)){
                        redisCache.setCacheList(key, list);
                    }
                    return AjaxResult.success(list);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.error("查找异常代码数据异常！！！");
            }finally {
                //释放锁
                locks.unlock();
            }
        }
        return AjaxResult.success(cacheList);
    }

    /**
     * 导出异常代码管理列表
     */
    @PreAuthorize("@ss.hasPermi('business:code:export')")
    @Log(title = "异常代码管理:导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExceptionCode exceptionCode)
    {
        List<ExceptionCode> list = exceptionCodeService.selectExceptionCodeList(exceptionCode);
        ExcelUtil<ExceptionCode> util = new ExcelUtil<ExceptionCode>(ExceptionCode.class);
        util.exportExcel(response, list, "异常代码管理数据");
    }

    /**
     * 获取异常代码管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('business:code:query')")
    @GetMapping(value = "/query/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(exceptionCodeService.selectExceptionCodeById(id));
    }

    /**
     * 新增异常代码管理
     */
    @PreAuthorize("@ss.hasPermi('business:code:add')")
    @Log(title = "异常代码管理:新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ExceptionCode exceptionCode)
    {
        return toAjax(exceptionCodeService.insertExceptionCode(exceptionCode));
    }

    /**
     * 修改异常代码管理
     */
    @PreAuthorize("@ss.hasPermi('business:code:update')")
    @Log(title = "异常代码管理:修改", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody ExceptionCode exceptionCode)
    {
        return toAjax(exceptionCodeService.updateExceptionCode(exceptionCode));
    }

    /**
     * 删除异常代码管理
     */
    @PreAuthorize("@ss.hasPermi('business:code:remove')")
    @Log(title = "异常代码管理:删除", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(exceptionCodeService.deleteExceptionCodeByIds(ids));
    }

}

